package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Holyn on 2017/7/12.
 */

public class ProductTypeFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private MyListAdapter listAdapter;
    private List<ItemVo> itemVos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_type, container, false);
        unbinder = ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initData() {

        for (int i = 1; i < 47; i++) {
            ItemVo itemVo;
            if (i == 46){
                itemVo = new ItemVo("全选", true);
            } else {
                if (i < 10) {
                    itemVo = new ItemVo("0" + i, false);
                } else {
                    itemVo = new ItemVo("" + i, false);
                }
            }
            itemVos.add(itemVo);

        }
        listAdapter = new MyListAdapter(itemVos);
    }

    private void initView() {
        rvList.setLayoutManager(new GridLayoutManager(getActivity(), 7));
        rvList.addItemDecoration(new GridSpacingItemDecoration(7, 10, false));
//        rvList.setAdapter(listAdapter);
        listAdapter.bindToRecyclerView(rvList);
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        String result = listAdapter.getCheckedResult();
        if (!TextUtils.isEmpty(result)){
            Intent intent = getActivity().getIntent();
            intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE, result);
            intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE_TYPE, ProductClassfiyActivity.TYPE_TYPE_CODE);
            getActivity().setResult(Activity.RESULT_OK, intent);
        }
        getActivity().finish();
    }

    private class MyListAdapter extends BaseQuickAdapter<ItemVo, BaseViewHolder> {
        private boolean onBind;
        private List<Boolean> isCheckList = new ArrayList<>();

        public MyListAdapter(@Nullable List<ItemVo> itemVos) {
            super(R.layout.item_product_type, itemVos);
            isCheckList.clear();
            for (int i = 0 ; i < itemVos.size(); i++){
                isCheckList.add(false);
            }
        }

        @Override
        protected void convert(BaseViewHolder helper, ItemVo item) {
            onBind = true;
            CheckBox cbText = helper.getView(R.id.cb_text);
            cbText.setText(item.getValue());
            cbText.setChecked(item.getIsCheck());
            if (item.getIsCheck()){
                cbText.setTextColor(Color.WHITE);
            } else {
                cbText.setTextColor(Color.BLACK);
            }
            onBind = false;
            cbText.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!onBind){
                        int position = helper.getAdapterPosition();
                        if (position == getItemCount()-1){//最后一个按钮
                            if (isChecked){
//                            buttonView.setText("全选");
                                getItem(position).setValue("全选");
                            } else {
//                            buttonView.setText("重置");
                                getItem(position).setValue("重置");
                            }

                            for (int i = 0; i < getItemCount()-1; i++){
                                getItem(i).setCheck(!isChecked);
                            }


                        }
                        getItem(position).setCheck(isChecked);
                        notifyDataSetChanged();
                    }
                }

            });
        }

        public String getCheckedResult(){
            boolean isCurSelectAll = true;
            String result = "";
            for (int i = 0; i < getItemCount()-1; i++){
                ItemVo itemVo = getItem(i);
                if (itemVo.getIsCheck()){
                    if (TextUtils.isEmpty(result)){
                        result = result + itemVo.getValue();
                    } else {
                        result = result + ";" + itemVo.getValue();
                    }
                } else {
                    isCurSelectAll = false;
                }
            }
            if (isCurSelectAll){
                return "全类";
            } else {
                return result;
            }

        }

    }

    private class ItemVo{
        private String value;
        private boolean isCheck;
        public ItemVo(String value, boolean isCheck) {
            this.value = value;
            this.isCheck = isCheck;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public boolean getIsCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }
    }

}
